<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <style>
        .my-layout-writeArticle {
            margin-top: 24px;
        }
    </style>
    <title>写文章</title>
</head>

<body>
    <div class="layui-container my-layout-writeArticle">
        <form id="articleForm" lay-filter="articleForm" class="layui-form" action="javascript:;">
            <div class="layui-form-item" pane>
                <input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                    class="layui-input">
            </div>
            <div class="layui-form-item" pane>
                <textarea name="content" id="writeArticle" style="display: none;"></textarea>
            </div>

            <div class="layui-form-item" pane>
                <label class="layui-form-label">文章标签:</label>
                <div class="layui-input-block">
                    <select name="tagId" lay-verify="required" lay-search>
                        <option value="">请选择</option>
                        <option th:each="tag:${tags}" th:text="${tag.name}"  th:value="${tag.id}"></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item" pane>
                <label class="layui-form-label">文章类型:</label>
                <div class="layui-input-block">
                    <select name="type1" lay-verify="required">
                        <option value="">请选择</option>
                        <option value="0">原创</option>
                        <option value="1">转载</option>
                        <option value="2">翻译</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" pane>
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="draft">保存草稿</button>
                    <button class="layui-btn" lay-submit lay-filter="article">发布文章</button>
                </div>
            </div>
        </form>
    </div>
</body>
<script src="/jquery-3.5.1.js"></script>
<script src="/layui/layui.js"></script>
<script th:inline="javascript">
    var article = [[${article}]];
    layui.use(['form', 'layedit', 'layer'], function () {
        var form = layui.form;
        var layedit = layui.layedit;
        var layer = layui.layer;

        layedit.set({
            uploadImage: {
                url: '/common/sysfile/upload',
                type: 'post'
            },
        });

        // 如果编辑草稿，给表单赋值
        if (article){
            //给表单赋值
            form.val("articleForm", {
                "title": article.title
                ,"tagId": article.tagId
                ,"type1": article.type1
            });
            $("#writeArticle").val(article.content);
        }

        // layedit如果赋值，要先赋值后渲染
        var index = layedit.build('writeArticle');

        // 监听-保存草稿
        form.on('submit(draft)', function (data) {
            data.field.content = layedit.getContent(index);
            data.field.isPublish = 0;
            data.field.userId = article.userId;
            data.field.id = article.id;

            $.ajax({
                url: '/article/article/update',
                type: 'post',
                // 同步，方便控制跳转
                async: false,
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(data.field),
                success: function (obj) {
                    if (obj.code == 200){
                        layer.open({
                            type: 0,
                            content: "草稿保存成功"
                        });
                    }else {
                        layer.open({
                            type: 0,
                            content: "草稿保存失败"
                        });
                    }
                }
            });

            window.location.href="/index";
        });

        // 监听-发布文章
        form.on('submit(article)', function (data) {
            data.field.content = layedit.getContent(index);
            data.field.isPublish = 1;
            data.field.userId = article.userId;
            data.field.id = article.id;

            $.ajax({
                url: '/article/article/update',
                type: 'post',
                // 同步，方便控制跳转
                async: false,
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(data.field),
                success: function (obj) {
                    if (obj.code == 200){
                        layer.open({
                            type: 0,
                            content: "文章发布成功"
                        });
                    }else{
                        layer.open({
                            type: 0,
                            content: "文章发布失败"
                        });
                    }
                }
            });

            window.location.href="/index";
        });
    });

</script>

</html>